import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import './assets/iconfont/iconfont.css'


Vue.prototype.axios = axios;
Vue.config.productionTip = false

// 导入ElementUI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// 安装
Vue.use(ElementUI);
// 导入axios
import axios from 'axios';
import VueAxios from 'vue-axios';

// 响应拦截器
axios.interceptors.response.use(
  function (res) {
    // console.log(res);
    // alert('正确');
    if (res.data.Code != "000000") {
      if(res.data.Code === "999999"){
        app.$message({
          message: res.data.Message,
          type: "error",
          duration: "3000",
        });
        return undefined;
      }
      app.$message({
        message: res.data.message,
        type: "error",
        duration: "3000",
      });
      return undefined;
    }
    return res.data
  },
  function (err) {
    console.log(err);
    let msg = '服务器错误';
    switch (err.response.status) {
      case 401:
        msg = "未授权，3秒后返回";
        break;
      default:
        break;
    }
    app.$message({
      message: msg,
      type: "error",
      duration: "3000",
      onClose(){
        app.$router.push('/Login')
      }
    });
    return err
  }
)
// 请求拦截器
axios.interceptors.request.use(
  function(config){
    config.headers["auth"]=localStorage.getItem("token");
    return config
  }
),function(err){
  return Promise.reject(err);
}
// 安装
Vue.use(VueAxios, axios)


var app = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
